class Solution {
public:
    bool isHappy(int n) {
        int sum = 0;
        bool f = 0;
        map<int, int>mp;
        while (1) {
            int t = 0;
            while (n) {
                int x = n % 10;
                t += x * x;
                n /= 10;
            }
            //cout<<"t=="<<t<<endl;
            if (mp[t])break;
            mp[t] = 1;
            n = t;
            if (n == 1) {
                f = 1;
                break;
            }
        }
        if (f)return 1;
        return 0;
    }
};